package uk.ac.lkl.common.util;



import java.util.EventListener;

import javax.swing.event.EventListenerList;


/**
 * This class forms a basis for handling registration of listeners for a
 * specific class of <code>EventObject</code>s.
 * <p>
 * Derived classes should supply the appropriate event firing methods specific
 * to the type of listener registered with this class.
 * <p>
 * Adapted from <code>javax.swing.event.SwingPropertyChangeSupport</code> and
 * <code>javax.swing.BoundedRangeModel</code>.
 * <p>
 * 
 * @author Richard Siddons-Corby
 * @author Darren Pearce (minor changes)
 * @version $Revision: 358 $
 * @version $Date: 2007-10-30 16:17:18 +0000 (Tue, 30 Oct 2007) $
 * 
 */
public abstract class EventSupport<L extends EventListener> extends
        AbstractEventSupport<L> {

    private final EventListenerList listenerList;


    /**
     * Construct event support for a given event source and listener type.
     * <p>
     * 
     * @param source
     *            the source for any events
     * @param listenerClass
     *            the class of event listener
     */
    protected EventSupport(Object source) {
        super(source);
        listenerList = new EventListenerList();
    }


    /**
     * Add a <code>EventListener</code>.
     * <p>
     * 
     * @param l
     *            the listener to add
     * @see #removeListener
     */
    public void addListener(L l) {
        listenerList.add(listenerClass, l);
    }


    /**
     * Remove a <code>EventListener</code>.
     * <p>
     * 
     * @param l
     *            the listener to remove
     * @see #addListener
     */
    public void removeListener(L l) {
        listenerList.remove(listenerClass, l);
    }


    /**
     * Return an array of all the listeners registered.
     * <p>
     * 
     * @return all of the listeners or an empty array if none are registered
     * @see #addListener
     * @see #removeListener
     */
    public L[] getListeners() {
        return getListeners(listenerList);
    }

}